<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>开闭原则 - 软件设计之美</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background: #f5f7fa;
            border-left: 4px solid #6e8efb;
        }
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.05);
        }
        .highlight {
            position: relative;
        }
        .highlight:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 30%;
            background: rgba(167, 119, 227, 0.2);
            z-index: -1;
            transition: all 0.3s ease;
        }
        .highlight:hover:after {
            height: 100%;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4">
        <div class="container mx-auto max-w-5xl text-center">
            <div class="inline-block px-4 py-2 mb-4 bg-white bg-opacity-20 rounded-full">
                <span class="text-sm font-semibold tracking-wider">软件设计原则</span>
            </div>
            <h1 class="text-4xl md:text-5xl font-bold mb-6">开闭原则</h1>
            <p class="text-xl md:text-2xl font-light max-w-3xl mx-auto leading-relaxed">
                "软件实体应该对扩展开放，对修改关闭" —— 优雅系统设计的核心原则
            </p>
            <div class="mt-10 flex justify-center space-x-4">
                <a href="#explanation" class="px-6 py-3 bg-white text-purple-700 font-medium rounded-full hover:bg-opacity-90 transition-all">
                    <i class="fas fa-book-open mr-2"></i>深入了解
                </a>
                <a href="#example" class="px-6 py-3 bg-transparent border-2 border-white text-white font-medium rounded-full hover:bg-white hover:bg-opacity-10 transition-all">
                    <i class="fas fa-code mr-2"></i>查看示例
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-5xl px-4 py-16">
        <!-- Definition Section -->
        <section id="definition" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                    <i class="fas fa-lightbulb text-purple-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">原则定义</h2>
            </div>
            <div class="bg-white p-8 rounded-xl shadow-sm border border-gray-100">
                <p class="text-xl leading-relaxed mb-6">
                    <span class="highlight font-medium">开闭原则</span>指的是"软件实体（如类、模块、函数等）应该对扩展开放，对修改关闭。" 也就是说，系统应该允许在不修改现有代码的情况下进行功能的扩展。
                </p>
                <div class="grid md:grid-cols-2 gap-8 mt-8">
                    <div class="bg-blue-50 p-6 rounded-lg">
                        <div class="flex items-center mb-3">
                            <div class="w-8 h-8 rounded-full bg-blue-100 flex items-center justify-center mr-3">
                                <i class="fas fa-external-link-alt text-blue-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">对扩展开放</h3>
                        </div>
                        <p class="text-gray-700">系统设计应支持通过添加新功能来扩展现有系统。这通常意味着可以通过继承、实现接口或其他方式来添加新功能，而不必更改原有的代码。</p>
                    </div>
                    <div class="bg-green-50 p-6 rounded-lg">
                        <div class="flex items-center mb-3">
                            <div class="w-8 h-8 rounded-full bg-green-100 flex items-center justify-center mr-3">
                                <i class="fas fa-lock text-green-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">对修改关闭</h3>
                        </div>
                        <p class="text-gray-700">修改现有代码可能会引入新的错误或副作用，因此在设计系统时，应尽量避免更改已有的代码。相反，应该通过扩展现有的代码来实现新的需求或功能。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Impact Section -->
        <section id="impact" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                    <i class="fas fa-chart-line text-indigo-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">对代码设计的影响</h2>
            </div>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="feature-card bg-white p-6 rounded-xl shadow-sm border border-gray-100 transition-all duration-300">
                    <div class="text-purple-600 text-3xl mb-4">
                        <i class="fas fa-tools"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">提升系统的可维护性</h3>
                    <p class="text-gray-700">通过遵循开闭原则，系统的现有部分在功能扩展时不会受到影响，从而减少了对现有代码的修改。这使得系统在演进和维护时更加稳定和可靠。</p>
                </div>
                <div class="feature-card bg-white p-6 rounded-xl shadow-sm border border-gray-100 transition-all duration-300">
                    <div class="text-blue-600 text-3xl mb-4">
                        <i class="fas fa-random"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">增强代码的灵活性</h3>
                    <p class="text-gray-700">遵循开闭原则的设计使得系统可以容易地应对需求变化或新功能的增加。新功能可以通过扩展而不是修改现有功能来实现。</p>
                </div>
                <div class="feature-card bg-white p-6 rounded-xl shadow-sm border border-gray-100 transition-all duration-300">
                    <div class="text-green-600 text-3xl mb-4">
                        <i class="fas fa-puzzle-piece"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">支持模块化设计</h3>
                    <p class="text-gray-700">开闭原则促进了模块化设计，每个模块或类都可以在不影响其他模块的情况下进行扩展。这种模块化设计有助于实现更好的代码组织和重用。</p>
                </div>
            </div>
        </section>

        <!-- Diagram Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-teal-100 flex items-center justify-center mr-4">
                    <i class="fas fa-project-diagram text-teal-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">开闭原则图解</h2>
            </div>
            <div class="mermaid">
                classDiagram
                    class ReportGenerator {
                        -reports: List~Report~
                        +generateReports()
                    }
                    class Report {
                        <<interface>>
                        +generate()
                    }
                    class PdfReport {
                        +generate()
                    }
                    class ExcelReport {
                        +generate()
                    }
                    class WordReport {
                        +generate()
                    }
                    Report <|-- PdfReport
                    Report <|-- ExcelReport
                    Report <|-- WordReport
                    ReportGenerator o-- Report
            </div>
            <p class="text-gray-600 mt-4 text-center italic">通过接口和多态实现开闭原则的设计模式</p>
        </section>

        <!-- Example Section -->
        <section id="example" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-orange-100 flex items-center justify-center mr-4">
                    <i class="fas fa-code text-orange-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">代码示例</h2>
            </div>
            
            <div class="mb-12">
                <h3 class="text-2xl font-semibold mb-4 text-gray-800">初始设计 (违反OCP)</h3>
                <div class="code-block p-6 rounded-lg overflow-x-auto">
                    <pre class="text-sm text-gray-800"><code>public class ReportGenerator {
    public void generateReport(String type) {
        if (type.equals("PDF")) {
            // 生成 PDF 报告
        } else if (type.equals("Excel")) {
            // 生成 Excel 报告
        }
    }
}</code></pre>
                </div>
                <div class="mt-4 p-4 bg-red-50 border-l-4 border-red-500">
                    <div class="flex">
                        <div class="flex-shrink-0">
                            <i class="fas fa-exclamation-circle text-red-500 mt-1"></i>
                        </div>
                        <div class="ml-3">
                            <p class="text-sm text-red-700">
                                问题：每次添加新报告类型都需要修改ReportGenerator类，违反了开闭原则。
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <div>
                <h3 class="text-2xl font-semibold mb-4 text-gray-800">改进设计 (遵循OCP)</h3>
                <div class="code-block p-6 rounded-lg overflow-x-auto">
                    <pre class="text-sm text-gray-800"><code>// 报告生成接口
public interface Report {
    void generate();
}

// PDF 报告实现
public class PdfReport implements Report {
    @Override
    public void generate() {
        // 生成 PDF 报告的逻辑
    }
}

// Excel 报告实现
public class ExcelReport implements Report {
    @Override
    public void generate() {
        // 生成 Excel 报告的逻辑
    }
}

// 报告生成器
public class ReportGenerator {
    private List&lt;Report&gt; reports;

    public ReportGenerator(List&lt;Report&gt; reports) {
        this.reports = reports;
    }

    public void generateReports() {
        for (Report report : reports) {
            report.generate();
        }
    }
}</code></pre>
                </div>
                <div class="mt-4 p-4 bg-green-50 border-l-4 border-green-500">
                    <div class="flex">
                        <div class="flex-shrink-0">
                            <i class="fas fa-check-circle text-green-500 mt-1"></i>
                        </div>
                        <div class="ml-3">
                            <p class="text-sm text-green-700">
                                改进：现在要添加新报告类型只需实现Report接口并添加到ReportGenerator中，无需修改现有代码。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Benefits Section -->
        <section>
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                    <i class="fas fa-star text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">开闭原则的优势</h2>
            </div>
            <div class="bg-white p-8 rounded-xl shadow-sm border border-gray-100">
                <ul class="space-y-6">
                    <li class="flex items-start">
                        <div class="flex-shrink-0 h-8 w-8 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                            <i class="fas fa-shield-alt text-purple-600"></i>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold mb-1">减少修改风险</h3>
                            <p class="text-gray-700">不需要修改现有代码来添加新功能，减少了引入新错误的风险。</p>
                        </div>
                    </li>
                    <li class="flex items-start">
                        <div class="flex-shrink-0 h-8 w-8 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                            <i class="fas fa-expand text-blue-600"></i>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold mb-1">提高代码可扩展性</h3>
                            <p class="text-gray-700">通过扩展系统而不是修改现有代码，使得系统更具适应性。</p>
                        </div>
                    </li>
                    <li class="flex items-start">
                        <div class="flex-shrink-0 h-8 w-8 rounded-full bg-green-100 flex items-center justify-center mr-4">
                            <i class="fas fa-book-reader text-green-600"></i>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold mb-1">提升代码可读性</h3>
                            <p class="text-gray-700">清晰地分离了扩展点和修改点，增强了代码的可理解性。</p>
                        </div>
                    </li>
                </ul>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col items-center">
                <div class="mb-4">
                    <span class="text-xl font-medium text-white">技术小馆</span>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-400 hover:text-white transition-colors">
                        <i class="fas fa-globe mr-2"></i>www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
            }
        });
    </script>
</body>
</html>